package com.hj.javaweb.init;

import com.hj.javaweb.entity.City;
import com.hj.javaweb.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 将城市的信息写入到redis中
 */
@Component
@Slf4j
public class DataInit implements ApplicationRunner {
    
    @Autowired
    private CityService cityService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    
    @Override
    public void run(ApplicationArguments args) {
        List<City> list = cityService.list();
        for (City city : list) {
            // 将城市信息写入到redis中，并设置过期时间是3分钟
            redisTemplate.opsForValue().set(city.getCityCode(),city,30, TimeUnit.MINUTES);
        }
    }
}
